//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.farm.resume.service.impl;

import com.farm.core.auth.domain.LoginUser;
import com.farm.core.sql.query.DataQuery;
import com.farm.core.time.TimeTool;
import com.farm.doc.server.FarmFileManagerInter;
import com.farm.doc.server.FarmFileManagerInter.FILE_APPLICATION_TYPE;
import com.farm.resume.dao.ResumebaseDaoInter;
import com.farm.resume.domain.Resumebase;
import com.farm.resume.service.ResumebaseServiceInter;
import java.util.List;
import javax.annotation.Resource;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class ResumebaseServiceImpl implements ResumebaseServiceInter {
	@Resource
	private ResumebaseDaoInter resumebaseDaoImpl;
	@Resource
	private FarmFileManagerInter farmFileManagerImpl;
	private static final Logger log = Logger.getLogger(ResumebaseServiceImpl.class);

	public ResumebaseServiceImpl() {
	}

	@Transactional
	public Resumebase insertResumebaseEntity(Resumebase entity, LoginUser user) {
		this.validateMobileAndEmail(entity);
		entity.setCuser(user.getId());
		entity.setCtime(TimeTool.getTimeDate14());
		entity.setCusername(user.getName());
		entity.setEuser(user.getId());
		entity.setEusername(user.getName());
		entity.setEtime(TimeTool.getTimeDate14());
		entity.setPstate("1");
		this.farmFileManagerImpl.submitFile(entity.getPhotoid(), FILE_APPLICATION_TYPE.RESUME_PHOTO.getValue());
		return this.resumebaseDaoImpl.insertEntity(entity);
	}

	private void validateMobileAndEmail(Resumebase entity) {
		List<String> userids = this.resumebaseDaoImpl.selectUserByMobile(entity.getMobilecode());
		if (userids.size() <= 1 && (userids.size() != 1 || ((String) userids.get(0)).equals(entity.getUserid()))) {
			userids = this.resumebaseDaoImpl.selectUserByEmail(entity.getEmailcode());
			if (userids.size() > 1 || userids.size() == 1 && !((String) userids.get(0)).equals(entity.getUserid())) {
				throw new RuntimeException("该电子邮件已经存在！");
			}
		} else {
			throw new RuntimeException("该手机号已经存在！");
		}
	}

	@Transactional
	public Resumebase editResumebaseEntity(Resumebase entity, LoginUser user) {
		this.validateMobileAndEmail(entity);
		Resumebase entity2 = this.resumebaseDaoImpl.getEntity(entity.getId());
		String oldFileid = entity2.getPhotoid();
		String newFileid = entity.getPhotoid();
		entity2.setEuser(user.getId());
		entity2.setEusername(user.getName());
		entity2.setEtime(TimeTool.getTimeDate14());
		entity2.setRegistered(entity.getRegistered());
		entity2.setDateyear(entity.getDateyear());
		entity2.setDatemonth(entity.getDatemonth());
		entity2.setRegisteredarea(entity.getRegisteredarea());
		entity2.setBirthday(entity.getBirthday());
		entity2.setSex(entity.getSex());
		entity2.setPcontent(entity.getPcontent());
		entity2.setUserid(entity.getUserid());
		entity2.setDegreemax(entity.getDegreemax());
		entity2.setName(entity.getName());
		entity2.setLivestrarea(entity.getLivestrarea());
		entity2.setLivestr(entity.getLivestr());
		entity2.setQqcode(entity.getQqcode());
		entity2.setWxcode(entity.getWxcode());
		entity2.setMobilecode(entity.getMobilecode());
		entity2.setEmailcode(entity.getEmailcode());
		entity2.setMarriagesta(entity.getMarriagesta());
		entity2.setNationality(entity.getNationality());
		entity2.setOthertype(entity.getOthertype());
		entity2.setOtherid(entity.getOtherid());
		entity2.setIdcode(entity.getIdcode());
		entity2.setStudyabroad(entity.getStudyabroad());
		entity2.setPhotoid(entity.getPhotoid());
		entity2.setZzmm(entity.getZzmm());
		entity2.setPhonecode(entity.getPhonecode());
		this.resumebaseDaoImpl.editEntity(entity2);
		this.farmFileManagerImpl.updateFileState(oldFileid, newFileid, user);
		return entity2;
	}

	@Transactional
	public void deleteResumebaseEntity(String id, LoginUser user) {
		this.resumebaseDaoImpl.deleteEntity(this.resumebaseDaoImpl.getEntity(id));
	}

	@Transactional
	public Resumebase getResumebaseEntity(String id) {
		return id == null ? null : this.resumebaseDaoImpl.getEntity(id);
	}

	@Transactional
	public DataQuery createResumebaseSimpleQuery(DataQuery query) {
		DataQuery dbQuery = DataQuery.init(query, "FARM_RESUME_BASE",
				"ID,REGISTERED,DATEYEAR,DATEMONTH,REGISTEREDAREA,BIRTHDAY,SEX,PCONTENT,USERID,NAME,PSTATE,EUSER,EUSERNAME,ETIME,CUSERNAME,CUSER,CTIME,LIVESTRAREA,LIVESTR,QQCODE,WXCODE,MOBILECODE,EMAILCODE,MARRIAGESTA,NATIONALITY,OTHERTYPE,OTHERID,IDCODE,STUDYABROAD,ZZMM");
		return dbQuery;
	}

	@Transactional
	public Resumebase getResumebaseEntityByUser(String userid) {
		return this.resumebaseDaoImpl.getResumebaseEntityByUser(userid);
	}

	@Transactional
	public String getUseridByEmailOrMobile(String emailOrMobile) {
		List<String> userids = this.resumebaseDaoImpl.selectUserByEmail(emailOrMobile);
		if (userids.size() > 0) {
			return (String) userids.get(0);
		} else {
			userids = this.resumebaseDaoImpl.selectUserByMobile(emailOrMobile);
			return userids.size() > 0 ? (String) userids.get(0) : null;
		}
	}
}
